/*
 * NetTCPReceiver.cpp
 *
 *  Created on: Apr 27, 2011
 *      Author: jacek
 */

#include "NetTCPReceiver.h"

NetTCPReceiver::NetTCPReceiver() {
	package.CreateReceiverPackage(sizeof(Event));
}
NetTCPReceiver::NetTCPReceiver(Socket &src) {
	receiverSock = src;
	package.CreateReceiverPackage(sizeof(Event));
}
NetTCPReceiver::~NetTCPReceiver() {

}

void NetTCPReceiver::Intialization() {

}

void NetTCPReceiver::ConvertEventNetToHost()
{
    eventBuf->destination = ntohl(eventBuf->destination);
    eventBuf->sender = ntohl(eventBuf->sender);
    eventBuf->type = ntohl(eventBuf->type);
}

void NetTCPReceiver::Updata(Event event) {
	if (event.type == NET_RECV){
		receiverSock.Recev(&package);
		eventBuf = reinterpret_cast<Event*> (package.buffor);
		ConvertEventNetToHost();
		SendEvent(*eventBuf);
	}
}

void NetTCPReceiver::SendEvent(Event event) {
	parent->SendEvent(event);
}

void NetTCPReceiver::Dispose() {
	eventBuf=NULL;
}
